package com.jia.together.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.*;
import com.jia.together.entity.MemberComment;
import com.jia.together.mapper.MemberCommentMapper;
import com.jia.together.service.BlogService;
import com.jia.together.service.CourseService;
import com.jia.together.service.MemberCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.together.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-24
 */
@Service
public class MemberCommentServiceImpl extends ServiceImpl<MemberCommentMapper, MemberComment> implements MemberCommentService {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private BlogService blogService;

    @Override
    public Map<String, Object> selectPageList(Long pageNum, Long pageSize) {

        Page<MemberComment> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MemberComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        Page<MemberComment> resourcePage = baseMapper.selectPage(page, wrapper);
        List<MemberComment> records = resourcePage.getRecords();
        records.forEach(item->{
            Member member = memberService.getById(item.getMemberId());
            if (member!=null){
                item.setMemberName(member.getNickname());
            }
            Blog blog = blogService.getById(item.getBlogId());
            if (blog!=null){
                item.setBlogName(blog.getTitle());
            }
            Course course = courseService.getById(item.getCourseId());
            if (course!=null){
                item.setCourseName(course.getTitle());
            }

        });
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return map;

    }

    @Override
    public Map<String, Object> selectListByBlogId(String courseId,String blogId, Long pageNum, Long pageSize) {

        Page<MemberComment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<MemberComment> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(blogId)){
            wrapper.eq("blog_id",blogId);
        }
        if(!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        Page<MemberComment> memberCommentPage = baseMapper.selectPage(page, wrapper);
        List<MemberComment> records = memberCommentPage.getRecords();
        records.forEach(item->{
            Member member = memberService.getById(item.getMemberId());
            item.setMemberName(member.getNickname());
            item.setMemberAvatar(member.getAvatar());
        });
        long total = memberCommentPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return map;

    }
}
